(function(D){
    D.initRelations = function(){
	var relations = [];
	var terms = [];
	var term = null;
	var subjects = [];
	var objects = [];

	$.get((location.protocol + "//" + location.host + "/getRelations.php"),
	      function(data){
		  var rows = JSON.parse(data);
		  var qualifiedRelationName;
		  var autocompleteList = [];
		  for(var i = 0; i < rows.length; i+=1){
		      qualifiedRelationName = decodeURIComponent(rows[i]["vocabulary_label"] + ":" + rows[i]["relation_label"]);
		      relations[qualifiedRelationName] = rows[i]["relation"];
		      if(!autocompleteList.hasElem(qualifiedRelationName)){
			  autocompleteList.push(qualifiedRelationName);
		      }
		  }
		  D.autocomplete(autocompleteList, document.getElementById("relationLabel"), function(item){
		      if(relations[item]){
			  document.getElementById("relationId").setAttribute("value", relations[item]);
		      }else{
			  document.getElementById("relationId").setAttribute("value", "");
		      }
		  });
	      });
	$.get((location.protocol + "//" + location.host + "/getTerms.php"),
	      function(data){
		  var rows = JSON.parse(data);
		  var qualifiedTermName;
		  var autocompleteList = [];
		  for(var i = 0; i < rows.length; i+=1){
		      qualifiedTermName = decodeURIComponent(rows[i]["class_label"] + ":" + rows[i]["term_label"]);
		      terms[qualifiedTermName] = rows[i]["term"];
		      if(!autocompleteList.hasElem(qualifiedTermName)){
			  autocompleteList.push(qualifiedTermName);
		      }
		  }
		  D.autocomplete(autocompleteList, document.getElementById("termLabel"), function(item){
		      if(terms[item]){
			  term = { "type": "term", "label": item, "id":terms[item] };
		      }else{
			  term = { "type": "term", "label": item, "id":"" };
		      }
		      objects.push(term);	
		      document.getElementById("object").setAttribute("value", JSON.stringify(objects));
		  });
	      });

	document.getElementById("postRelations").onsubmit = function(){
	    if(subjects === [] ||
	       objects  === [] ||
	       document.getElementById("relationLabel").value === ""){
		return false;
	    }else{
		return true;
	    }
	};

	document.getElementById("clearBtn").onmouseup = function(){
	    subjects = [];
	    objects = [];
	    document.getElementById("subject").setAttribute("value", JSON.stringify(subjects));
	    document.getElementById("object").setAttribute("value", JSON.stringify(objects));
	    document.getElementById("relationLabel").value = "";
	    document.getElementById("relationId").setAttribute("value", "");
	    document.getElementById("termLabel").value = "";
	};

	document.getElementById("subjectLabel").onmouseup = function(){
	    if(D.dragObject.type){
		subjects.push({
		    "type":  D.dragObject.type,
		    "id": 	 D.dragObject.id,
		    "thumb": D.dragObject.thumb,
		    "big": 	 D.dragObject.big
		});
		document.getElementById("subject").setAttribute("value", JSON.stringify(subjects));
		document.getElementById("editRelationsSubject").setAttribute("value",
									     D.dragObject.id);
	    }
	}

	document.getElementById("objectLabel").onmouseup = function(){
	    if(D.dragObject.type){
		objects.push({
		    "type":  D.dragObject.type,
		    "id": 	 D.dragObject.id,
		    "thumb": D.dragObject.thumb,
		    "big": 	 D.dragObject.big
		});
		document.getElementById("object").setAttribute("value", JSON.stringify(objects));
	    }
	}

	document.getElementById("user").setAttribute("value", D.userName());
	document.getElementById("editRelationsUser").setAttribute("value", D.userName());
    }
})(DESANTI);
